using System;
using System.Collections;
using Server;
using Server.Spells;
using Server.Mobiles;
using Server.Network;

namespace Server.Engines.Doom
{
	public class LampRoomBox : Item
	{
		private LeverPuzzleController m_Controller;
		private Mobile m_Wanderer;

		public LampRoomBox( LeverPuzzleController controller ) : base( 0xe80 )
		{
			m_Controller = controller;
			ItemID = 0xe80;
			Movable = false;
		}

		public override void OnDoubleClick( Mobile m )
		{
			if ( !m.InRange( this.GetWorldLocation(), 3 ) )
				return;
			if ( m_Controller.Enabled )
				return;

			if ( (m_Wanderer == null || !m_Wanderer.Alive) )
			{
				m_Wanderer = new WandererOfTheVoid();
				m_Wanderer.MoveToWorld( LeverPuzzleController.lr_Enter, Map.Malas );
				m_Wanderer.PublicOverheadMessage( MessageType.Regular, 0x3B2, 1060002, "" ); // I am the guardian of...
				Timer.DelayCall( TimeSpan.FromSeconds( 5.0 ), new TimerCallback( CallBackMessage ) );
			}
		}

		public void CallBackMessage()
		{
			PublicOverheadMessage( MessageType.Regular, 0x3B2, 1060003, "" ); // You try to pry the box open...
		}
		public override void OnAfterDelete()
		{
			if( m_Controller!=null && !m_Controller.Deleted )
				m_Controller.Delete();
		}
		public LampRoomBox( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			writer.Write( m_Controller );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			m_Controller = reader.ReadItem() as LeverPuzzleController;
		}
	}

	public class LeverPuzzleStatue : Item
	{
		private LeverPuzzleController m_Controller;

		public LeverPuzzleStatue( int[] dat, LeverPuzzleController controller ) : base( dat[0] )
		{
			m_Controller=controller;
			Hue = 0x44E;
			Movable = false;
		}
		public override void OnAfterDelete()
		{
			if( m_Controller!=null && !m_Controller.Deleted )
				m_Controller.Delete();
		}
		public LeverPuzzleStatue( Serial serial ) : base( serial )
		{
		}
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			writer.Write( m_Controller );
		}
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			m_Controller = reader.ReadItem() as LeverPuzzleController;
		}
	}

	public class LeverPuzzleLever : Item
	{
		private UInt16 m_Code;
		private LeverPuzzleController m_Controller;

		[CommandProperty( AccessLevel.GameMaster )]
		public UInt16 Code
		{
			get{ return m_Code; }
		}

		public LeverPuzzleLever( UInt16 code, LeverPuzzleController controller ) : base( 0x108E )
		{
			m_Controller=controller;
			m_Code = code;
			Hue = 0x66D;
			Movable = false;
		}

		public override void OnDoubleClick( Mobile m )
		{
			if ( m != null && m_Controller.Enabled )
			{
				ItemID^=2;
				Effects.PlaySound( Location, Map, 0x3E8 );
				m_Controller.LeverPulled( m_Code );
			}
			else
			{
				m.SendLocalizedMessage( 1060001 ); // You throw the switch, but the mechanism cannot be engaged again so soon.
			}
		}

		public override void OnAfterDelete()
		{
			if( m_Controller != null && !m_Controller.Deleted )
				m_Controller.Delete();
		}

		public LeverPuzzleLever( Serial serial ) : base( serial )
		{
		}
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			writer.Write( (ushort) m_Code );
			writer.Write( m_Controller );
		}
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			m_Code = reader.ReadUShort();
			m_Controller = reader.ReadItem() as LeverPuzzleController;
		}
	}

    [TypeAlias( "Server.Engines.Doom.LampRoomTelePorter" )]
	public class LampRoomTeleporter : Item
	{
		public LampRoomTeleporter( int[] dat )
		{
			Hue = dat[1];
			ItemID = dat[0];
			Movable = false;
		}

		public override bool HandlesOnMovement { get { return true; } }
		public override bool OnMoveOver( Mobile m )
		{
			if( m != null && m is PlayerMobile )
			{
				if ( SpellHelper.CheckCombat( m ) )
				{
					m.SendLocalizedMessage( 1005564, "", 0x22 ); // Wouldst thou flee during the heat of battle??				
				}
				else
				{
					Server.Mobiles.BaseCreature.TeleportPets( m, LeverPuzzleController.lr_Exit, Map.Malas );
					m.MoveToWorld( LeverPuzzleController.lr_Exit, Map.Malas );
					return false;
				}
			}
			return true;
		}

		public LampRoomTeleporter( Serial serial ) : base( serial )
		{
		}
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
		}
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
		}
	}
}


